<?
session_start();

require("../classCol.php");
require("../helpers/CHtml.php");
require("../helpers/CJavascript.php");
require("../models/GFetchers.php");
require("../models/GHandlers.php");
require("controllers/local.php");
require("models/fetchers.php");
require("models/handlers.php");

$thisModul = "RECV-QTY";

$srcDb     = "USERID_DTL";

//check if the object of entry is exist if doesnt then create one
//First opening
if( !$_POST["entry"]    && 
    !$_POST["addrow"]   && 
    !$_POST["confirm"]  && 
    !$_POST["back"]     && 
    !$_POST["droprow"]  && 
    !$_POST["view"]     && 
    !$_POST["cat_pick"] ){
	
	/**
	* We dont have anything to Pick
	* so we are by pass the category pick
	*
	*/
	require("entry_category_pick.php");
	
	//require("entry_data_body.php");
}

/**
* if supplier picked then continue
*
*/
if($_POST["cat_pick"]){
	
	require("entry_data_body.php");
}

//if entry or confirm or back selected
if( $_POST["entry"]   || 
    $_POST["confirm"] || 
    $_POST["back"]    ){
	
	/**
	* check if the one has priviledge to entry
	*
	*/
	$UID = unserialize($_SESSION["serUID"]);
	
	$COracle = new oracleDb;
	
	$COracle->init($_SESSION["ORCLU"],
	               $_SESSION["OPASS"],
	               $_SESSION["SID"],
	               _NO_COMMENT);
	               
  if($COracle->connect()){
	  
  	if(chk_priv($COracle, 
                "ENTRY_RIGHTS", 
                $UID->getUID(), 
                $thisModul,
                $srcDb) == "OK"){
  	                          
      $COracle->dc();
  		
  		require("entry_details_check.php");
  	}else{
  		
  		usrError("Authed Error reason :".$chk_res." ");
  	}
  	
  	$COracle->dc();
  }else{
    
    usrDie("Oracle Connection failed");
  }
}

if($_POST["addrow"]){
	
	require("entry_add_row.php");
}

/**
* Presquities to use this item are:
*
* 1. to use dropBoxEntp[] auto numbering index
*    don't do it manually
* 2. The value of the dropBox should 
*    take normal numbering instead of array numbering
*
*/
if($_POST["droprow"]){
	
	//var_dump($_POST["dropBox"]);
	
	/**
  * splice the dummy
  * The first 2 params are to shrink the array
  * e.g: array_splice($dummy, 2); is to shrink dummy to 2 elements
  *
  * The last 2 params is to concanate the array
  * it tells how many array to concat and the content of the array
  *
  * dropPoint needs to be reduce by 1 so we could have the cursor
  * of array for $dummy at the correct posisition
  * e.g: dropPoint = 3, meaning the 3rd elements is going to be dust off
  *      so we only save the first and 2nd element. Then use splice
  *      to concat the 4th...nElement with the first 2 element.
  *
  */
  
	/**
	* get how many selected drops
	*
	*/
	$dropTimes = sizeof($_POST["dropBox"]);
	
	//usrDebug("dropTimes",$dropTimes);
	
	//list all records name
	$lstArray = array("odrDateEnt",
	                  "odrRefEnt",
	                  "odrNoEnt",
	                  "catEnt",
	                  "grpEnt",
	                  "modelEnt",
	                  "unitEnt",
	                  "qtyEnt", 
	                  "cvtEnt",
	                  "cvtQtyEnt",
	                  "recvQtyEnt",
	                  "priceEnt",
	                  "hrzTotEnt");
	
	                  
	//shift it for many times
	foreach($lstArray as $list){
		
		for($i = $dropTimes ; $i > 0 ; $i--){
			
			/**
			* Since $_POST["dropBox"] index is using array numbering
			* then we need to reduce the droppoint by 1.
			* E.g: $dropTimes = 3 => $_POST["dropBox"][3] will result Error
			*      since the dropTimes is trying to point to 3rd element
			*      of $_POST["dropBox"], which array wise is at [2] meaning 3rd element
			*
			*/ 
			$dropPoint = $_POST["dropBox"][($i-1)];
			
			/**
			* slice it! so we could have the buffer's tail
			*
			*/
			$bufTail   = array_slice($_POST[$list], $dropPoint);
			
			//var_dump($bufTail);
			
			//replace it
			array_splice($_POST[$list],               /* The original array or dummy */
			            ($dropPoint - ARRAY_NUMBER),  /* Calculate the cursor, since it comes with array numbering */
			            sizeof($bufTail),             /* The sizeof array concat with */
			            $bufTail);                    /* The array we concat with */
			
			//var_dump($_POST[$list]);
		}
	}
	unset($list);
	
	$new_init_num = $_SESSION["init_num"] - $dropTimes;
	//echo	"<br/>\$new_init_num :".$new_init_num ;
	if($new_init_num >= 0)
		
		$_SESSION["init_num"] = $new_init_num;
	else
		
		$_SESSION["init_num"] = 0;
	
	require("entry_restore_me.php");
}

echo "<br/><br/><br/><br/>";
?>
